import { reactive, ref } from 'vue'

export function useForgotPassword() {
  // 表单数据
  const form = reactive({
    phone: '',
    verifyCode: '',
    newPassword: '',
    confirmPassword: '',
  })

  // 状态管理
  const loading = ref(false)
  const sendingCode = ref(false)
  const countdown = ref(0)
  const currentStep = ref(1) // 1: 验证手机号, 2: 设置新密码
  const showNewPassword = ref(false)
  const showConfirmPassword = ref(false)

  // Logo图片
  const logoImage = ref('/static/logo.png')

  // 手机号验证
  function validatePhone(phone: string): boolean {
    const phoneRegex = /^1[3-9]\d{9}$/
    return phoneRegex.test(phone)
  }

  // 验证码验证
  function validateVerifyCode(code: string): boolean {
    return /^\d{6}$/.test(code)
  }

  // 密码验证
  function validatePassword(password: string): boolean {
    return password.length >= 6
  }

  // 发送验证码
  async function sendVerifyCode() {
    if (!form.phone.trim()) {
      uni.showToast({
        title: '请输入手机号',
        icon: 'none',
      })
      return
    }

    if (!validatePhone(form.phone)) {
      uni.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
      })
      return
    }

    sendingCode.value = true

    try {
      // 模拟发送验证码API
      await new Promise(resolve => setTimeout(resolve, 1000))

      uni.showToast({
        title: '验证码已发送',
        icon: 'success',
      })

      // 开始倒计时
      startCountdown()
    }
    catch (error: any) {
      uni.showToast({
        title: error?.message || '发送失败，请重试',
        icon: 'none',
      })
    }
    finally {
      sendingCode.value = false
    }
  }

  // 开始倒计时
  function startCountdown() {
    countdown.value = 60
    const timer = setInterval(() => {
      countdown.value--
      if (countdown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  }

  // 验证手机号步骤
  async function verifyPhone() {
    if (!form.phone.trim()) {
      uni.showToast({
        title: '请输入手机号',
        icon: 'none',
      })
      return
    }

    if (!validatePhone(form.phone)) {
      uni.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
      })
      return
    }

    if (!form.verifyCode.trim()) {
      uni.showToast({
        title: '请输入验证码',
        icon: 'none',
      })
      return
    }

    if (!validateVerifyCode(form.verifyCode)) {
      uni.showToast({
        title: '请输入6位数验证码',
        icon: 'none',
      })
      return
    }

    loading.value = true

    try {
      // 模拟验证手机号API
      await new Promise(resolve => setTimeout(resolve, 1000))

      uni.showToast({
        title: '验证成功',
        icon: 'success',
      })

      // 进入下一步
      currentStep.value = 2
    }
    catch (error: any) {
      uni.showToast({
        title: error?.message || '验证失败',
        icon: 'none',
      })
      form.verifyCode = ''
    }
    finally {
      loading.value = false
    }
  }

  // 重置密码
  async function resetPassword() {
    if (!form.newPassword.trim()) {
      uni.showToast({
        title: '请输入新密码',
        icon: 'none',
      })
      return
    }

    if (!validatePassword(form.newPassword)) {
      uni.showToast({
        title: '密码至少6位字符',
        icon: 'none',
      })
      return
    }

    if (!form.confirmPassword.trim()) {
      uni.showToast({
        title: '请确认新密码',
        icon: 'none',
      })
      return
    }

    if (form.newPassword !== form.confirmPassword) {
      uni.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
      })
      return
    }

    loading.value = true

    try {
      // 模拟重置密码API
      await new Promise(resolve => setTimeout(resolve, 1000))

      uni.showToast({
        title: '密码重置成功',
        icon: 'success',
      })

      // 返回登录页
      setTimeout(() => {
        uni.redirectTo({ url: '/pages/login/index' })
      }, 1500)
    }
    catch (error: any) {
      uni.showToast({
        title: error?.message || '重置失败',
        icon: 'none',
      })
    }
    finally {
      loading.value = false
    }
  }

  // 返回上一步
  function goBack() {
    if (currentStep.value === 1) {
      uni.navigateBack()
    }
    else {
      currentStep.value = 1
    }
  }

  // 跳转到登录页
  function goToLogin() {
    uni.redirectTo({ url: '/pages/login/index' })
  }

  // 切换密码显示状态
  function toggleNewPasswordVisibility() {
    showNewPassword.value = !showNewPassword.value
  }

  function toggleConfirmPasswordVisibility() {
    showConfirmPassword.value = !showConfirmPassword.value
  }

  return {
    form,
    loading,
    sendingCode,
    countdown,
    currentStep,
    showNewPassword,
    showConfirmPassword,
    logoImage,
    sendVerifyCode,
    verifyPhone,
    resetPassword,
    goBack,
    goToLogin,
    toggleNewPasswordVisibility,
    toggleConfirmPasswordVisibility,
  }
}
